#!/bin/bash

# 参数校验
if [ ${1}x == ""x ]; then
  echo "缺少工程名称，请在命令后面补充工程的全名。例：“./restart.sh bd-domp-service test”"
  exit 0
fi
if [ ${2}x == ""x ]; then
  echo "缺少环境参数，请在命令后面补充上环境参数。例：“./restart.sh bd-domp-service test”"
  exit 0
fi
if [ ${2}x != "test"x -a ${2}x != "prod"x  ]; then
  echo "环境参数只能为test或prod"
  exit 0
fi

# 查询jar工程线程ID
process_id=`ps -ef | grep ${1}.jar |grep -v grep |grep -v restart | awk -F ' ' '{print $2}'`


# 未运行
if [ ${process_id}x == ""x ]; then
  echo "该工程没有运行"
#  nohup /data/apps/jdk1.8.0_101/bin/java -jar -jar /data/daps-model-biz/daps-model-biz.jar  --spring.profiles.active=test >/dev/null 2>&1 &
  nohup /data/apps/jdk1.8.0_101/bin/java -jar -jar /data/${1}/${1}.jar  --spring.profiles.active=${2} >/dev/null 2>&1 &
	
# 运行中  
else
  echo "该jar工程正在运行中，线程ID为：“${process_id}”"
  kill  ${process_id}
  echo "停止工程成功，等待3秒后再次启动项目"
  sleep 3
  nohup /data/apps/jdk1.8.0_101/bin/java -jar -jar /data/${1}/${1}.jar  --spring.profiles.active=${2} >/dev/null 2>&1 &		
fi
  sleep 1
  process_id=`ps -ef | grep ${1}.jar |grep -v grep |grep -v restart | awk -F ' ' '{print $2}'`
  echo "启动完毕,线程ID为：“${process_id}”"
exit 0
